package com.ruoyi.web.controller.wx;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ByNews;
import com.ruoyi.system.service.IByNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/wx/news")
@Validated
public class WxNewsController extends BaseController {

    @Autowired
    private IByNewsService newsService;


    @PostMapping("indexNews")
    public AjaxResult indexNews(){
        List<ByNews> list = newsService.indexNews();//.indexNews();
//        System.out.println(list.toString());
        return AjaxResult.success(list);
    }


    @PostMapping("pagesNews")
    public AjaxResult pagesNews(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);


        List<ByNews> list = newsService.pagesNews(json);//.indexNews();
//        System.out.println(list.toString());
        return AjaxResult.success(list);
    }



    @PostMapping("readNews")
    public AjaxResult readNews(@RequestBody String param){
        JSONObject json = JSONObject.parseObject(param);
        String id = json.getString("id");
        /*JSONObject news = newsService.getNewsById(id);
//        System.out.println(news.toString());
        return success(news.toString());*/
        ByNews news = newsService.selectByNewsById(id);
        return AjaxResult.success(news);
    }
}
